using FytSoa.Domain.Crm;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Crm;

/// <summary>
/// 工单日志表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v7")]
public class CrmWocLogService : IApplicationService 
{
    private readonly SugarRepository<CrmWocLog> _thisRepository;
    public CrmWocLogService(SugarRepository<CrmWocLog> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<List<CrmWocLogDto>> GetListAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .Where(m=>m.OrderId==param.Id)
            .OrderByDescending(m=>m.Id)
            .ToListAsync();
        return query.Adapt<List<CrmWocLogDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<CrmWocLogDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<CrmWocLogDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task AddAsync(CrmWocLog model)
    {
        model.EmployeeId = AppUtils.LoginId;
        var loginName = AppUtils.LoginUser;
        model.Remark = model.Status switch
        {
            1 => loginName + " 创建了新的工单",
            2 => loginName + " 更新了工单信息",
            3 => loginName + " 工单流转指派给他人",
            4 => loginName + " 接收了工单任务",
            5 => loginName + " 更新了工单状态为：待审核",
            6 => loginName + " 驳回了工单",
            7 => loginName + " 更新了工单状态为：已完成",
            8 => loginName + " 关闭了工单",
            9 => loginName + " 更新了工单的处理限时",
            _ => model.Remark
        };
        await _thisRepository.InsertAsync(model);
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(CrmWocLogDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<CrmWocLog>());

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
